import Vue from 'vue'
import VueRouter from 'vue-router'


import Home from '../views/frontdesk/Home.vue'

Vue.use(VueRouter)
//解决路由跳转到同一节点下报错的问题
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {

return originalPush.call(this, location).catch(err => err)

}

const routes = [{
        name: 'Home',
        path: '/',
        //此时有一个问题：进入“/”后maincontent中没有内容，操作为在home.vue中创建完成后通过路由跳转到子路由index
        component: Home,
        redirect:'/Index',
        children:[
            {
                name:'Index',
                path:'/Index',
                component:()=>import('../views/frontdesk/index.vue')

            },
            {
                name:'NewsCategory',
                path:'/NewsCategory/:id',
                component:()=>import('../components/frontdesk/newscategorymaincontent.vue')

            },
            {
                name:'News',
                path:'/News/:id',
                component:()=>import('../components/frontdesk/newsmaincontent.vue')

            },
            //前台用户登陆管理模块
            {
                name:'User',
                path:'/User',
                component:()=>import('../components/frontdesk/User/UserLayout.vue'),
                redirect:'/User/UserIndex',
                children:[
                    {
                        // 首页欢迎页面
                        name:'UserIndex',
                        path:'/User/UserIndex',
                        component:()=>import('../components/frontdesk/User/Index.vue')
                    },
                    {
                        name:'NewsManage',
                        path:'/User/NewsManage',
                        component:()=>import('../components/frontdesk/User/NewsManage.vue'),
                        redirect:'/User/NewsManage/NewsInShow',
                        children:[
                            {
                                name:'NewsInShow',
                                path:'/User/NewsManage/NewsInShow',
                                component:()=>import('../components/frontdesk/User/News/NewsInShow.vue'),
                                
                            },
                            {
                                name:'NewsToAudit',
                                path:'/User/NewsManage/NewsToAudit',
                                component:()=>import('../components/frontdesk/User/News/NewsToAudit.vue')
                            },
                            {
                                name:'Add',
                                path:'/User/NewsManage/Add',
                                component:()=>import('../components/frontdesk/User/News/Add.vue')
                            },
                        ]
                    },
                    {
                        name:'UserManage',
                        path:'/User/UserManage',
                        component:()=>import('../components/frontdesk/User/UserManage/UserManage.vue'),
                        redirect:'/User/UserManage/ChangeInfo',
                        children:[
                            {
                                name:'ChangeUserInfo',
                                path:'/User/UserManage/ChangeInfo',
                                component:()=>import('../components/frontdesk/User/UserManage/ChangeInfo.vue'),
                                
                            },
                            {
                                name:'ChangePassword',
                                path:'/User/UserManage/ChangePassword',
                                component:()=>import('../components/frontdesk/User/UserManage/ChangePassword.vue')
                            },
                            {
                                name:'Logout',
                                path:'/User/UserManage/Logout',
                                component:()=>import('../components/frontdesk/User/UserManage/Logout.vue')
                            },
                        ]
                    },
                ]

            },
            
        ]

    },
    // login
    {
        name:'Login',
        path:'/Login',
        component:()=>import('../views/frontdesk/Login.vue')

    },
    // Register
    {
        name:'Register',
        path:'/Register',
        component:()=>import('../views/frontdesk/Register.vue')

    },
    {
        name: 'backdesk',
        path: '/backdesk',
        // component: Home,

    },
    // test
    {
        name: 'testUpload',
        path: '/test/upload',
        component: ()=>import('../components/test/upload.vue'),

    },
    {
        name: 'deletebyid',
        path: '/test/deletebyid',
        component: ()=>import('../components/test/deletebyId.vue'),

    },
    {
        name: 'testCors',
        path: '/test/Cors',
        component: ()=>import('../components/test/Cors.vue'),

    },
    // {
    //     name: 'testMd',
    //     path: '/test/Md',
    //     component: ()=>import('../components/MDinput/index.vue'),

    // },


]

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash', // require service support
    base: process.env.BASE_URL,
    routes
})

// 添加路由导航守卫：使用户在未登录的状态下无法访问User及之下的URL
router.beforeEach((to, from, next) => {
    var isLogin=window.sessionStorage.getItem('token')?true:false
    // 判断如果当前页面为需要登录的页面:/User
    // console.log(to.path.slice(0,5));
    var userURL=to.path.slice(0,5);
    // console.log(isLogin);
    // console.log(userURL);
    if(userURL==='/User'){
        if(isLogin){
            next()
        }
        // 如果没有登录,则回到之前的页面
        else{
            next(from.path)
        }
    }
    next();
  })

export default router